//
//  EnergyConsumptionBenchmarking.m
//  cocoapodText
//
//  Created by apple on 16/9/10.
//  Copyright © 2016年 apple. All rights reserved.
//


#import "ShiPei.h"
#import <Foundation/Foundation.h>
#import "EnergyConsumptionBenchmarking.h"
@interface EnergyConsumptionBenchmarking ()<ApiEnergyInfoManagerDelegate,GYEnergyBenchmarkingViewDelegate>

@end

@implementation EnergyConsumptionBenchmarking

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:FUIColorFromRGB(0xf4f4f4)];
    [self GetMainContentView];
}


/**
 * 边界
 * top:(0, 0, kScreenW, 10)
 * bottom:(0, kScreenH-10, kScreenW,10)
 * left:(0, 0, 10, kScreenH)
 * right:(kScreenW-170, 0, 10, kScreenH)
 */
-(void)GetMainContentView{
    //内容栏
    _energyBenchmarkingView=[[EnergyBenchmarkingView alloc]initWithFrame:CGRectMake(0, 0, [ShiPei CGFloatMakeScale:TabChildContentW+10], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:FSCommonBottomH])];
    [self.view addSubview:_energyBenchmarkingView];
    [_energyBenchmarkingView setDelegate:self];
    
}

-(void)initEnergyData{
    ApiEnergyInfoManager *manager=[[ApiEnergyInfoManager alloc]init];
    [manager setDelegate:self];
    //初始化
    [manager fsgy_api_energy_benchmarking_info_withRequestCode:ECBInit];
    [_energyBenchmarkingView setInitCondition];
}

-(void)onInitFristChartTableData:(EnergyBenchmarkingView *)energyBenchmarkingView esCode:(NSString *)esCode{
    ApiEnergyInfoManager *manager=[[ApiEnergyInfoManager alloc]init];
    [manager setDelegate:self];
    NSString *contrastName=@"单位面积";
    NSString *contrastType=@"2";
    //默认曲线，表格(贵阳市：电：月：一个月内数据)
    
    [manager fsgy_api_energy_benchmarking_info_withRequestCode:ECBChart esCode:esCode orgid:@"520100000:4:贵阳市:00" structType:@"4" xslx:@"2" startTime:[CommonTool getDateTimeDifferenceWithYear:0 Month:-1 Day:0 Rule:@"yyyyMM"] contrastType:contrastType contrastName:contrastName];
}

-(void)onQueryChartData:(EnergyBenchmarkingView *)energyBenchmarkingView esCode:(NSString *)esCode xslx:(NSString *)xslx startTime:(NSString *)startTime orgid:(NSString *)orgid structType:(NSString *)structType contrastType:(NSString *)contrastType contrastName:(NSString *)contrastName{
    ApiEnergyInfoManager *manager=[[ApiEnergyInfoManager alloc]init];
    [manager setDelegate:self];
    [manager fsgy_api_energy_benchmarking_info_withRequestCode:ECBChart esCode:esCode orgid:orgid structType:structType xslx:xslx startTime:startTime contrastType:contrastType contrastName:contrastName];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}

-(void)onEnergyInfo:(ApiEnergyInfoManager *)manager result:(NSDictionary *)result code:(NSString *)code{
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    if ([code isEqualToString:ECBInit]) {
        //分项行业目录
        NSArray *lstTree=[result objectForKey:@"lstTree"];
        [_energyBenchmarkingView setGYMenuTableData:lstTree];
        //能源类型
        NSArray *typeMapList=[result objectForKey:@"typeMapList"];
        [_energyBenchmarkingView setGYMenuEnergyTypeData:typeMapList];
    }else if([code isEqualToString:ECBChart]){
        [self onSetBarChartData:_energyBenchmarkingView BaseInfo:result];
        //表格数据
        NSArray *tableBodyObj=[result objectForKey:@"tableBodyObj"];
        NSDictionary *tableHeaderObj=[result objectForKey:@"tableHeaderObj"];
        NSString *title=[CommonTool getStringFromNSDictionary:result key:@"title" defaultValue:@""];
        [_energyBenchmarkingView setEnergyTableWithTitle:title header:tableHeaderObj Data:tableBodyObj];
    }
}

-(void)onSetBarChartData:(EnergyBenchmarkingView *)energyBenchmarkingView BaseInfo:(NSDictionary *)data{
    //chart数据
    NSArray *xAxis=[data objectForKey:@"xAxis"];
    NSArray *series=[data objectForKey:@"series"];
    NSArray *legend=[data objectForKey:@"legend"];
    
    NSMutableArray *xVals = [[NSMutableArray alloc] init];
    for (int i = 0; i < [xAxis count]; i++){
        [xVals addObject:[CommonTool getStringFromNSArray:xAxis key:i]];
    }
    NSMutableArray *yVals = [[NSMutableArray alloc] init];
    NSMutableArray *barColors = [[NSMutableArray alloc] init];
    
    
    NSMutableArray *yVal01 = [[NSMutableArray alloc] init];
    NSMutableArray *yVal02 = [[NSMutableArray alloc] init];
    NSMutableArray *yVal03 = [[NSMutableArray alloc] init];
    NSMutableArray *yVal04 = [[NSMutableArray alloc] init];
    
    for (int i=0; i<[series count]; i++) {
        NSDictionary *temDic=[series objectAtIndex:0];
        NSArray *temArray=[temDic objectForKey:@"data"];
        NSString *temValStr=[CommonTool getStringFromNSArray:temArray key:i];
        double val=0;
        if ([temValStr isEqualToString:@"-"]||[temValStr isEqualToString:@"--"]) {
            val=0;
        }else{
            val=[temValStr doubleValue];
        }
        [yVal01 addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:i]];
    }
    
    for (int i=0; i<[series count]; i++) {
        NSDictionary *temDic=[series objectAtIndex:1];
        NSArray *temArray=[temDic objectForKey:@"data"];
        NSString *temValStr=[CommonTool getStringFromNSArray:temArray key:i];
        double val=0;
        if ([temValStr isEqualToString:@"-"]||[temValStr isEqualToString:@"--"]) {
            val=0;
        }else{
            val=[temValStr doubleValue];
        }
        [yVal02 addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:i]];
    }
    
    for (int i=0; i<[series count]; i++) {
        NSDictionary *temDic=[series objectAtIndex:2];
        NSArray *temArray=[temDic objectForKey:@"data"];
        NSString *temValStr=[CommonTool getStringFromNSArray:temArray key:i];
        double val=0;
        if ([temValStr isEqualToString:@"-"]||[temValStr isEqualToString:@"--"]) {
            val=0;
        }else{
            val=[temValStr doubleValue];
        }
        [yVal03 addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:i]];
    }
    
    for (int i=0; i<[series count]; i++) {
        NSDictionary *temDic=[series objectAtIndex:3];
        NSArray *temArray=[temDic objectForKey:@"data"];
        NSString *temValStr=[CommonTool getStringFromNSArray:temArray key:i];
        double val=0;
        if ([temValStr isEqualToString:@"-"]||[temValStr isEqualToString:@"--"]) {
            val=0;
        }else{
            val=[temValStr doubleValue];
        }
        [yVal04 addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:i]];
    }
    [yVals addObject:yVal01];
    [yVals addObject:yVal02];
    [yVals addObject:yVal04];
    [yVals addObject:yVal03];
    
    [barColors addObject:FUIColorFromRGB(0x1f73af)];
    [barColors addObject:FUIColorFromRGB(0x59cc9a)];
    [barColors addObject:FUIColorFromRGB(0xff3a00)];
    [barColors addObject:FUIColorFromRGB(0x1fcc00)];
    [barColors addObject:FUIColorFromRGB(0x593aaf)];
    [barColors addObject:FUIColorFromRGB(0xff739a)];
    [barColors addObject:FUIColorFromRGB(0xffccaf)];
    [barColors addObject:FUIColorFromRGB(0x1f3a9a)];
    [barColors addObject:FUIColorFromRGB(0x597300)];
    [barColors addObject:FUIColorFromRGB(0x43d282)];
    [barColors addObject:FUIColorFromRGB(0x45c8dc)];
    [barColors addObject:FUIColorFromRGB(0xf77d6a)];
    [barColors addObject:FUIColorFromRGB(0x9b6ed0)];
    [barColors addObject:FUIColorFromRGB(0xf76e82)];
    [barColors addObject:FUIColorFromRGB(0x457dd0)];
    [barColors addObject:FUIColorFromRGB(0x43c86a)];
    [barColors addObject:FUIColorFromRGB(0xf76e82)];
    [barColors addObject:FUIColorFromRGB(0x9b7ddc)];
    [barColors addObject:FUIColorFromRGB(0xf7c882)];
    [barColors addObject:FUIColorFromRGB(0x1f73af)];
    [barColors addObject:FUIColorFromRGB(0x9bc8af)];
    [barColors addObject:FUIColorFromRGB(0xf773dc)];
    [barColors addObject:FUIColorFromRGB(0x1f7d82)];
    [barColors addObject:FUIColorFromRGB(0x9b7382)];
    [barColors addObject:FUIColorFromRGB(0xf77daf)];
    [barColors addObject:FUIColorFromRGB(0x1fc8dc)];
    
    energyBenchmarkingView.xValsEnergyLine=xVals;
    energyBenchmarkingView.yValsEnergyLine=yVals;
    energyBenchmarkingView.barColors=barColors;
    energyBenchmarkingView.title=[CommonTool getStringFromNSDictionary:data key:@"title" defaultValue:@""];
    
    [energyBenchmarkingView multipleBarChartData:legend];
}

-(void)onSetData:(EnergyBenchmarkingView *)energyBenchmarkingView{
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self initEnergyData];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_energyBenchmarkingView dismissMenu];
}
@end
